package demo;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class EncodingDemo {
    private static int chart;

    public static void main(String[] args) {
        String originalStr = "沉默王二";
        String encodedStr = "";
        // 直接使用16进制字面量（以0x或0X开头）
        // 以"中"字为例：Unicode码点：U+4E2D (十进制 20013)
        int utf8Word = 0xE4B8AD;
        // 将int数字转换为二进制字节数组
        byte[] originalBytes = new byte[3];
        originalBytes[0] = (byte) (utf8Word >>> 16);  // 高位字节
        originalBytes[1] = (byte) (utf8Word >>> 8);   // 中位字节
        originalBytes[2] = (byte) utf8Word;           // 低位字节
        String chineseStr = "";

        int starCodePoint = 0x1F31F;  // 🌟 的Unicode码点
        byte[] starBytes = encodeToUTF8(starCodePoint);

        try {
            byte[] bytes = originalStr.getBytes("GBK");
            encodedStr = new String(bytes, StandardCharsets.UTF_8);
            chineseStr = new String(originalBytes, StandardCharsets.UTF_8);
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

        System.out.println("转码前: " + originalStr);
        System.out.println("转码后: " + encodedStr);
        System.out.println("==============================");
        System.out.println("UTF-8: " + chineseStr);
        // 显示字节数组内容
        System.out.println("位运算方法生成的字节数组是: " + bytesToHex(originalBytes));
        byte[] utf8Bytes = chineseStr.getBytes(StandardCharsets.UTF_8);
        System.out.printf("\"%s\" 字UTF-8字符编码的字节数组是: %s%n", chineseStr, bytesToHex(utf8Bytes));
        System.out.println("==============================");
        System.out.println("encodeToUTF8 方法生成的字节数组是: " + bytesToHex(starBytes));
        System.out.println("UTF-8: " + new String(starBytes, StandardCharsets.UTF_8));
    }

    /**
     * 手动UTF-8编码实现
     */
    public static byte[] encodeToUTF8(int codePoint) {
        if (codePoint <= 0x7F && codePoint >= 0) {
            // 1字节: 0xxxxxxx
            return new byte[]{(byte) codePoint};

        } else if (codePoint <= 0x7FF && codePoint >= 0) {
            // 2字节: 110xxxxx 10xxxxxx
            return new byte[]{
                    (byte) (0xC0 | (codePoint >> 6)),
                    (byte) (0x80 | (codePoint & 0x3F))
            };

        } else if (codePoint <= 0xFFFF && codePoint >= 0) {
            // 3字节: 1110xxxx 10xxxxxx 10xxxxxx
            return new byte[]{
                    (byte) (0xE0 | (codePoint >> 12)),
                    (byte) (0x80 | ((codePoint >> 6) & 0x3F)),
                    (byte) (0x80 | (codePoint & 0x3F))
            };

        } else {
            // 4字节: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
            return new byte[]{
                    (byte) (0xF0 | (codePoint >> 18)),
                    (byte) (0x80 | ((codePoint >> 12) & 0x3F)),
                    (byte) (0x80 | ((codePoint >> 6) & 0x3F)),
                    (byte) (0x80 | (codePoint & 0x3F))
            };
        }
    }

    /**
     * 将字节数组转换为十六进制字符串（用于显示）
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            result.append(String.format("%02X ", b));
        }
        return result.toString().trim();
    }
}
